package com.fuxue.controller.user;

import com.fuxue.constant.JwtClaimsConstant;
import com.fuxue.dto.*;
import com.fuxue.entity.AllFiled;
import com.fuxue.entity.Server;
import com.fuxue.entity.User;
import com.fuxue.properties.JwtProperties;
import com.fuxue.result.Result;
import com.fuxue.service.ServerService;
import com.fuxue.service.UserService;
import com.fuxue.utils.JwtUtil;
import com.fuxue.vo.ServerLoginVO;
import com.fuxue.vo.ServerVO;
import com.fuxue.vo.UserLoginVO;
import com.fuxue.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController("userHomeController")
@RequestMapping("/users/user/home")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);


        User user=userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, user.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .userId(user.getUserId())
                .userName(user.getUsername())
                .avatar(user.getAvatar())
               // .filed(filed)
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增用户
     * @param
     * @return
     */
    @PostMapping("/new")
    @ApiOperation("新增用户")
    public Result save(@RequestBody UserDTO userDTO){
        log.info("新增用户：{}",userDTO);
        userService.save(userDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户信息")
    public Result<UserVO> getById(@PathVariable Integer id) {
        log.info("根据id查询用户信息，{}", id);
        UserVO userVO = userService.getById(id);
        return Result.success(userVO);
    }
    /*@PutMapping("/filed")
    @ApiOperation("修改领域")
    public Result updateName(@RequestBody ServerUPFiledDTO serverUPFiledDTO) {
        log.info("修改服务者负责领域：{}", serverUPFiledDTO);
        serverService.updateWithFiled(serverUPFiledDTO);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key

        return Result.success();
    }*/
    @PutMapping("/name")
    @ApiOperation("修改用户的用户名")
    public Result updateName(@RequestBody UserUpNameDTO userUpNameDTO)
    {
        log.info("修改服务者用户名：{}", userUpNameDTO);
        userService.updateWithName(userUpNameDTO);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key

        return Result.success();
    }

    @PutMapping("/password")
    @ApiOperation("修改用户密码")
    public Result updatePassword(@RequestBody UserUpPasswordDTO userUpPasswordDTO)
    {
        log.info("修改用户密码：{}",userUpPasswordDTO);
        userService.updateWithPassword(userUpPasswordDTO);
        return Result.success();
    }

    @PutMapping("/image")
    @ApiOperation("修改用户头像")
    public Result updateImage(@RequestBody UserUpAdvateDTO userUpAdvateDTO)
    {
        log.info("修改用户头像：{}",userUpAdvateDTO);
        userService.updateWithAdvate(userUpAdvateDTO);
        return Result.success();
    }
    @GetMapping("/AllFiled")
    @ApiOperation("得到全部领域")
    public Result<List<AllFiled>>getFiled()
    {
        log.info("得到全部领域");
        List<AllFiled>list = userService.getAllFiled();


        return Result.success(list);
    }

}
